import { userLogin, fetchWhoAmi } from "@/api/user";
import { ElMessage } from "element-plus";
export default {
    namespaced: true,
    state: {
        user: {},
        isLoading: false
    },
    mutations: {
        // 设置User内容
        setUsers(state, payload) {
            state.user = payload;
        },
        // 设置加载中
        setIsLoading(state, payload) {
            state.isLoading = payload;
        }
    },
    actions: {
        /**
         * 
         *登录
         * @returns boolean 是否登录成功
         */
        async login({ commit, state }, payload) {
            commit("setIsLoading", true);
            const res = await userLogin(payload);
            if (res.msg) {
                ElMessage({
                    message: res.msg,
                    type: "error",
                    duration: 1500
                });
                return false;
            }
            commit("setUsers", res.data);
            commit("setIsLoading", false);
            return true;
        },
        /**
         * 
         * 我是谁
         * @returns 用户数据
         */
        async whoAmi({ commit, state }) {
            const res = await fetchWhoAmi();
            commit("setUsers", res.data);
            return res.data;
        },
        /**
         * 清除登录
         */
        removeLogin({ commit }) {
            localStorage.removeItem("UserToken");
            commit("setUsers", {})
        }
    }
}

